package com.pt.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pt.commonutils.R;
import com.pt.eduservice.entity.EduTeacher;
import com.pt.eduservice.entity.TeacherQuery;
import com.pt.eduservice.service.EduTeacherService;
import com.pt.eduservice.service.TeacherService;
import com.pt.exception.EduException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author pengtao
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/eduservice/edu-teacher/")
@Api(produces = "讲师管理")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @Autowired
    private TeacherService teacherService;

    @GetMapping("findAll")
    @ApiOperation(value = "查询所有讲师")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);

        return R.ok().data("list",list);
    }


    @DeleteMapping("{id}")
    @ApiOperation(value = "根据讲师id删除讲师，逻辑删除")
    public R removeTeacherById(
            @ApiParam(name = "id",value = "讲师id",required = true)
            @PathVariable("id") String id){

        eduTeacherService.removeById(id);

      return   R.ok();

    }


    @GetMapping("{current}/{limit}")
    @ApiOperation(value = "分页查询")
    public R pageList(
            @ApiParam(name = "current",value = "当前页",required = true)
            @PathVariable("current") Integer current,
            @ApiParam(name = "limit",value = "每页条数",required = true)
            @PathVariable("limit") Integer limit
    ){

        Page<EduTeacher> page = new Page<>(current,limit);

        eduTeacherService.page(page,null);

        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();

        Map<String,Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);


        return R.ok().data("items", map);
    }

    @PostMapping("{current}/{limit}")
    @ApiOperation(value = "条件分页查询")
    public R pageConditionList(
            @ApiParam(name = "current",value = "当前页",required = true)
            @PathVariable("current") Integer current,
            @ApiParam(name = "limit",value = "每页条数",required = true)
            @PathVariable("limit") Integer limit,
            @ApiParam(name = "teacherQuery",value = "封装的条件")
            @RequestBody(required = false)TeacherQuery teacherQuery
            ){

           Page<EduTeacher> page = new Page<>(current,limit);

           teacherService.pageQuery(page,teacherQuery);

        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();

        Map<String,Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        return R.ok().data(map);
    }


    @PostMapping("save")
    @ApiOperation(value = "新增讲师")
    public R saveEduTeacher(
            @ApiParam(name = "eduTeacher",value = "新增的讲师",required = true)
            @RequestBody EduTeacher eduTeacher
    ){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){

            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("selectById/{id}")
    @ApiOperation(value = "根据id查询讲师")
     public R selectById(
            @ApiParam(name = "id",value = "讲师id",required = true)
            @PathVariable("id") String id
){
    EduTeacher eduTeacher = eduTeacherService.getById(id);
try {
    int a = 10 / 0;//检测异常处理
}catch (Exception e){

    throw new EduException(20001,"自定义的异常");
}
    if (eduTeacher!=null){
        return R.ok().data("item",eduTeacher);
    }else {
        return R.error();
    }

}
    @PostMapping("updateById")
    @ApiOperation(value = "根据id修改讲师")
    public R updateById(
            @ApiParam(name = "eduTeacher",value = "要修改的讲师",required = true)
            @RequestBody EduTeacher eduTeacher
     ){

         boolean b = eduTeacherService.updateById(eduTeacher);
         if (b){
             return R.ok();
         }else {
             return R.error();
         }

     }



}

